import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
//import Mock from './mock'
//Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'
//--------引入axios相关内容----------
import axios from 'axios'

//配置axios的全局基本路径
axios.defaults.baseURL='/api'
//给Vue扩展一个属性$http
Vue.prototype.$http=axios;



Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    //NProgress.start();
    if (to.path == '/login') {
        localStorage.removeItem('token');
    }
    let token =localStorage.getItem('token');
    if (!token && to.path != '/login') {
        next({ path: '/login' })
    } else {
        next()
    }
})
/*
router.afterEach(transition => {
  console.debug(transition)
});
*/

//添加前置拦截器
axios.interceptors.request.use(config=>{
    //获取当前浏览器存储的token
    let token=localStorage.getItem("token");
    console.debug(token);
    if (token){//本地有存储的token
        config.headers["TOKEN"]=token;
    }

    return config;
},error => {
    Promise.reject(error);
})
// 如果没有登录 返回的数据 {"success":false,"message":"noAuth"}
axios.interceptors.response.use(config=>{
    console.debug(config)
    if(!config.data.success&&config.data.message==="noAuth"){
        //移除本地存储的数据
        localStorage.removeItem("token");
        localStorage.removeItem("loginInfo");

        //跳转到登录页面
        location.href="/#/login";
    }
    return config;
},error => {
    Promise.reject(error);
})

new Vue({
    //el: '#app',
    //template: '<App/>',
    router,
    store,
    //components: { App }
    render: h => h(App)
}).$mount('#app')

